﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Security.Windows.Forms;
using System.Windows.Forms;
using Gmail.Presenters;
using Gmail.Presenters.Events;

namespace Gmail.Views
{
    public class LoginForm : ILoginView
    {
        //TODO - need to find a way to "forget" a user.
        public LoginForm()
        {

        }
        public void FormInit()
        {
            Login(null,false,false);
        }

        public void FormInit(Boolean Relogin)
        {
            if (Relogin) this.Relogin(); else FormInit();
        }

        private void Relogin()
        {
            Login("Enter your username", false, true);
        }

        private void Login(String Name, Boolean IncorrectPassword, Boolean Relogin)
        {
            CredentialsDialog Dialog = new CredentialsDialog("gmail.com", "Login to Gmail","Please enter your credentials for your Google Account");
            if (IncorrectPassword)
            {
                Dialog.AlwaysDisplay = true;
                Dialog.IncorrectPassword = true;
            }
            if (Relogin) Dialog.AlwaysDisplay = true;

            if (Dialog.Show(Name) == DialogResult.OK)
            {
                if (LoginSubmitEvent != null)
                {
                    Cursor ThisCursor = Cursor.Current;
                    Cursor.Current = Cursors.WaitCursor;
                    Boolean Result = LoginSubmitEvent(this, new LoginSubmitEventArgs(Dialog.Name, Dialog.Password, Dialog.SaveChecked));
                    Cursor.Current = ThisCursor;
                    if (Result)
                    {
                        // If credentials are valid            
                        // and the user checked the "remember my password" option 
                        if (Dialog.SaveChecked)
                            Dialog.Confirm(true);
                        else
                        {
                            // Try to remove credentials
                            try
                            {
                                Dialog.Confirm(false);
                            }
                            catch
                            {
                            }
                        }
                    }
                    else
                    {
                        // Try to remove credentials 
                        try
                        {
                            Dialog.Confirm(false);
                        }
                        catch
                        {
                        }
                        Login(Dialog.Name,true,false);
                    }
                }

            }
            else
            {
                Dialog.Name = "Enter your username";
            }
        } 

        #region ILoginView Members

        public event LoginSubmitEventHandler LoginSubmitEvent;

        #endregion
    }
}
